#!/bin/bash

#

MYSQL_USER="root"
MYSQL_PASSWORD="Abcdefg@123"
MYSQL_HOST="127.0.0.1"
MYSQL_PORT="43306"

bak_pathg=/data/backup/upgradefiles
bak_pathu=/data/backup/updatefiles
bak_pathb=/data/backup/backupallfiles
bak_patho=/data/backup/other

OA_ROOT=/www/wwwroot/office
BLOG_ROOT=/www/wwwroot/blog
DEV_ROOT=/www/wwwroot/dev

mkdir -p  $bak_pathg $bak_pathu $bak_pathb $bak_patho
find $bak_pathb -maxdepth 1 -type f -exec rm -f {} \;

# 检测MySQL连接
if ! mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -P "$MYSQL_PORT" -e "SELECT 1" >/dev/null 2>&1; then
    # 连接失败时记录日志并退出
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] 错误：无法连接到MySQL服务器，请检查服务状态或配置！" | tee -a "$bak_pathb/errorlog.txt" >&2
    exit 1
fi

echo "备份mysql中......"
#这里的mysqldump路径修改成实际的路径、-u用户名 -p密码，都要修改
mysqldump -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -P "$MYSQL_PORT" gouguoa | gzip > $bak_pathb/gouguoa.sql.gz
mysqldump -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -P "$MYSQL_PORT" gougublog | gzip > $bak_pathb/gougublog.sql.gz
mysqldump -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -P "$MYSQL_PORT" gougudev | gzip > $bak_pathb/gougudev.sql.gz

# ========== 新增MySQL备份验证 ==========
validate_mysql_backup() {
  local file=$1
  if ! gzip -t "$file"; then
    echo "[错误] MySQL备份压缩包损坏: $file" >&2
    return 1
  fi
  if ! zgrep -q "Dump completed" "$file"; then
    echo "[错误] MySQL备份不完整: $file" >&2
    return 1
  fi
  echo "[验证通过] MySQL备份有效: $file"
  return 0
}

validate_mysql_backup "$bak_pathb/gouguoa.sql.gz" || exit 1
validate_mysql_backup "$bak_pathb/gougublog.sql.gz" || exit 1
validate_mysql_backup "$bak_pathb/gougudev.sql.gz" || exit 1

echo "文件备份中......"
echo "####" > "$OA_ROOT"/read.txt  #目的修改office文件夹日期
echo "####" >  "$BLOG_ROOT"/read.txt  #目的修改blog文件夹日期
echo "####" >  "$DEV_ROOT"/read.txt   #目的修改dev文件夹日期
rm -rf "$OA_ROOT"/read.txt "$BLOG_ROOT"/read.txt "$DEV_ROOT"/read.txt
tar -czf "$bak_pathb/office.tar.gz" -C "$(dirname "$OA_ROOT")" "$(basename "$OA_ROOT")"
tar -czf "$bak_pathb/blog.tar.gz" -C "$(dirname "$BLOG_ROOT")" "$(basename "$BLOG_ROOT")"
tar -czf "$bak_pathb/dev.tar.gz" -C "$(dirname "$DEV_ROOT")" "$(basename "$DEV_ROOT")"

# ========== 新增文件备份验证 ==========
validate_file_backup() {
  local file=$1
  if ! tar -tzf "$file" >/dev/null 2>&1; then
    echo "[错误] 文件备份损坏: $file" >&2
    return 1
  fi
  file_count=$(tar -tzf "$file" | wc -l)
  if [ "$file_count" -lt 3 ]; then
    echo "[警告] 备份文件数量异常少: $file (仅$file_count个文件)" >&2
  fi
  echo "[验证通过] 文件备份有效: $file (包含$file_count个文件)"
  return 0
}

validate_file_backup "$bak_pathb/office.tar.gz" || exit 1
validate_file_backup "$bak_pathb/blog.tar.gz" || exit 1
validate_file_backup "$bak_pathb/dev.tar.gz" || exit 1

echo "复制备份文件......"
backup_dir="$bak_pathb/backupallfiles_$(date +\%Y\%m\%d\%H\%M)"
mkdir -p "$backup_dir" || { echo "无法创建备份目录: $backup_dir"; exit 1; }
find "$bak_pathb" -maxdepth 1 -type f -exec cp -f {} "$backup_dir" \; || { echo "复制文件到 $backup_dir 失败"; exit 1; }

echo "备份backupallfiles完成"

# 
# 写入备注
echo "Only retain data from the past 3 days" > $bak_pathb/read.txt
echo "Only retain data from the past 3 days" > $bak_pathu/read.txt
echo "Only retain data from the past 3 days" > $bak_pathg/read.txt
echo "Only retain data from the past 3 days" > $bak_patho/read.txt
# 删除超过3天的文件
find $bak_pathb -mindepth 1 -maxdepth 1 -type d -mtime +2 -exec rm -rf {} \;
find $bak_pathb -mindepth 1 -maxdepth 1 -type f -mtime +2 -exec rm -rf {} \;
find $bak_pathb -mindepth 1 -maxdepth 1 -type d -ctime +2 -exec rm -rf {} \;
echo "删除backupallfiles超过3天的文件完成"

find $bak_pathu -mindepth 1 -maxdepth 1 -type d -mtime +2 -exec rm -rf {} \;
find $bak_pathu -mindepth 1 -maxdepth 1 -type f -mtime +2 -exec rm -rf {} \;
find $bak_pathu -mindepth 1 -maxdepth 1 -type d -ctime +2 -exec rm -rf {} \;
echo "删除updatefiles超过3天的文件完成"

find $bak_pathg -mindepth 1 -maxdepth 1 -type d -mtime +2 -exec rm -rf {} \;
find $bak_pathg -mindepth 1 -maxdepth 1 -type f -mtime +2 -exec rm -rf {} \;
find $bak_pathg -mindepth 1 -maxdepth 1 -type d -ctime +2 -exec rm -rf {} \;
echo "删除upgradefiles超过3天的文件完成"

find $bak_patho -mindepth 1 -maxdepth 1 -type d -mtime +2 -exec rm -rf {} \;
find $bak_patho -mindepth 1 -maxdepth 1 -type f -mtime +2 -exec rm -rf {} \;
find $bak_patho -mindepth 1 -maxdepth 1 -type d -ctime +2 -exec rm -rf {} \;
echo "删除other超过3天的文件完成"
#